package com.pac.role.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.role.bean.RoleUserRelBean;
import com.pac.role.dao.RoleUserRelDao;
import com.pac.role.service.RoleUserRelService;

/**
 * 用户角色关联关系Service实现
 * 
 * @author lujun.chen
 * @version [版本号, 2015年2月14日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "roleUserRelService")
public class RoleUserRelServiceImpl implements RoleUserRelService {

	private static Logger logger = Logger
			.getLogger(RoleUserRelServiceImpl.class);

	@Autowired
	private RoleUserRelDao roleUserRelDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addRoleUserRel(RoleUserRelBean roleUserRelBean) {

		if (null == roleUserRelBean) {
			logger.error("添加用户角色关联关系出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(roleUserRelBean.getRoleId())) {
			logger.error("添加用户角色关联关系出错，角色ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "角色ID为空");
		}

		if (StringTools.isNull(roleUserRelBean.getUserId())) {
			logger.error("添加用户角色关联关系出错，用户ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "用户ID为空");
		}

		return roleUserRelDao.addRoleUserRel(roleUserRelBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delRoleUserRel(RoleUserRelBean roleUserRelBean) {
		if (null == roleUserRelBean) {
			logger.error("删除用户角色关联关系出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(roleUserRelBean.getUserId())) {
			logger.error("删除用户角色关联关系出错，用户ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "用户ID为空");
		}
		return roleUserRelDao.delRoleUserRel(roleUserRelBean);
	}

	@Override
	public List<RoleUserRelBean> queryUserRoleByUserId(String userId) {
		if (StringTools.isNull(userId)) {
			logger.error("根据用户I的查询用户角色关联关系出错，用户ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL,
					"根据用户I的查询用户角色关联关系出错，用户ID为空");
		}

		RoleUserRelBean roleUserRelBean = new RoleUserRelBean();
		roleUserRelBean.setUserId(userId);
		return roleUserRelDao.queryRoleUserRel(roleUserRelBean);
	}

}
